// miniprogram/group_admin/pages/reportContent/reportContent.js
// 获取api
const api = require("../../../utils/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //默认为‘未掌握 0 薄弱 1 掌握 2 被选中’
    isTrue:0,
    studyNewList:"",
    isMessage:false,
    show: [
      [],
      [],
      []
    ],
    hidden:true
  },
  // 显示未掌握章节
 noStudy:function(){
  var that = this;
  var studyMOdelList=this.data.studyList;
  that.setData({
    isTrue: 0,
    studyNewList:studyMOdelList.unMasterList

  })
  },
    // 显示掌握章节
 yesStudy:function(){
  var that = this;
  var studyMOdelList=this.data.studyList;
  that.setData({
    isTrue: 2,
    studyNewList:studyMOdelList.masterList
  })
 },
// 显示薄弱章节
 brStudy:function(){
  var that = this;
  var studyMOdelList=this.data.studyList;
  that.setData({
    isTrue: 1,
    studyNewList:studyMOdelList.weakList
  })
  },
  // 是否单击了章节
  //点击知识点
  changeShow: function(e) {
    var sndid = e.currentTarget.dataset.index;
    var fstid = e.currentTarget.dataset.fstid;
    var show = this.data.show;
    if (show[fstid][sndid]) {
      show[fstid][sndid] = false;
    } else {
      show[fstid] = [];
      show[fstid][sndid] = true;
    }
    this.setData({
      "show": show
    });
  },
  //画布
  canvas() {
    //根据屏幕宽度设置圆半径
    var canvaswidth = wx.getSystemInfoSync().windowWidth * 260 / 750;
    //对应的颜色 以及百分点（数据）
    var data = this.data.messarr;
    var sum = data[0].flownum + data[1].flownum + data[2].flownum;

    var ctx = wx.createCanvasContext("Canvas");

    for (var i = 0; i < data.length; i++) {
      ctx.beginPath();
      var start = 0.03;
      if (i > 0) {
        start = 0;
        // 计算开始弧度是前几项的总和，即从之前的基础的上继续作画
        for (var j = 0; j < i; j++) {
          start += data[j].flownum / sum * 2 * Math.PI;
        }
      }
      if (i == 0) {
        ctx.arc(canvaswidth / 2, canvaswidth / 2, canvaswidth / 2, start, data[i].flownum / sum * 2 * Math.PI);
      } else {
        ctx.arc(canvaswidth / 2, canvaswidth / 2, canvaswidth / 2, start, data[i].flownum / sum * 2 * Math.PI + start);
      }
      ctx.lineTo(canvaswidth / 2, canvaswidth / 2);
      ctx.setFillStyle(data[i].color)
      ctx.fill()
      ctx.draw(true);
    }
    var ctx1 = wx.createCanvasContext("Canvas1");
    ctx1.beginPath();
    ctx1.arc(canvaswidth / 2, canvaswidth / 2, canvaswidth / 4, 0, 2 * Math.PI);
    ctx1.setFillStyle("#FFF")
    ctx1.fill()
    ctx1.draw(true);
  },

  // 绘图计时器
  countInterval: function () {
    var that=this;
    // 设置倒计时 定时器 每100毫秒执行一次，计数器count+1 ,耗时6秒绘一圈
    this.countTimer = setInterval(() => {
    if (this.data.count <= 60) {
    /* 绘制彩色圆环进度条
    注意此处 传参 step 取值范围是0到2，
    所以 计数器 最大值 60 对应 2 做处理，计数器count=60的时候step=2
    */
    that.canvas();
    this.data.count++;
    } else {
    clearInterval(this.countTimer); 
    }
    }, 100)
    },

  //单击学习报告 item type=17
  clickItem:async function(options){
    let that = this;
    let option = {
      unitid:options.currentTarget.dataset.unitid,
      kpid:options.currentTarget.dataset.kpid
    };
    let hidden = await api.clickItem(option);
    that.setData({
      hidden:hidden
    });

  //   var resYtkUserInfo= wx.getStorageSync("USERDATA");
  //   var courseInfo= wx.getStorageSync("COURSEINFO");
  //   // var moduleList= wx.getStorageSync("MODULELIST");
  //   var type=17;
  //   var unitid=e.currentTarget.dataset.unitid;
  //   // var kpid=e.currentTarget.dataset.kpid;
  //   const parmas={
  //     "sessionid": resYtkUserInfo.sessionid,
  //     "uid": resYtkUserInfo.uid,
  //     "courseid": courseInfo.id,
  //     "market": getApp().globalData.market,
  //     // "type": moduleList.module,
  //     "type": type,
  //     "unitid":unitid,
  //     from:getApp().globalData.from
  //   }
  //   //页面传参跳转
  //   wx.navigateTo({
  //   url: '../tiku/tiku?parmas='+JSON.stringify(parmas)
  //   })

  },


  /**
   * 生命周期函数--监听页面加载
   * 从学习报告页面获取的课程名称以及课程id
   */
  onLoad: function (options) {
    this.setData({
      'coursetitle':options.coursetitle,
    }),
    this.data.courseId=options.courseId
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var resYtkUserInfo=wx.getStorageSync('USERDATA');
    var params = {
      "sessionid": resYtkUserInfo.sessionid,
      "uid": resYtkUserInfo.uid,
      "courseId": this.data.courseId,
    };
    var url = "/myStudyCenter/findLearningReport";
    var that = this;
    api.sendPosts( url,params, function(res) {
      that.setData({
        "studyList": res,
        "studyNewList":res.unMasterList
        });
        
      //统计未掌握，薄弱，已掌握考点个数
      var master = 0;
      var weak = 0;
      var unMaster = 0;

      for (var i = 0; i < res.unMasterList.length; i++) {
        for (var j = 0; j < res.unMasterList[i].knowPointList.length; j++) {
          unMaster = unMaster + 1;
        }
      }
      for (var i = 0; i < res.weakList.length; i++) {
        for (var j = 0; j < res.weakList[i].knowPointList.length; j++) {
          weak = weak + 1;
        }
      }
      for (var i = 0; i < res.masterList.length; i++) {
        for (var j = 0; j < res.masterList[i].knowPointList.length; j++) {
          master = master + 1;
        }
      }

      that.setData({
        "master":master,
        "weak":weak,
        "unMaster":unMaster,
      })

      //画布开始
      // 获取百分点
      var unMasteredPercent = parseFloat(that.data.studyList.unMasteredPercent); //未掌握
      var masteredPecent = parseFloat(that.data.studyList.masteredPecent); //掌握
      var weakPecent = parseFloat(that.data.studyList.weakPecent); //薄弱

      // 设置圆环基本信息
      that.data.messarr = [{
          color: '#EDF1F5',
          flownum: unMasteredPercent,
        },
        {
          color: '#FF8212',
          flownum: masteredPecent,
        },
        {
          color: '#FECC34',
          flownum: weakPecent,
        }
      ];
      that.canvas();
      // that.countInterval();

    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})